package com.baoy.cloud.xxx.common;

import com.baoy.cloud.xxx.common.ex.Exceptions;
import lombok.Data;

@Data
public class JSONResult {

    Object data;
    Boolean success;
    String message;
    int code = 0;

    public static JSONResult writeData(Object data) {
        JSONResult ret = new JSONResult();
        ret.setData(data);
        ret.setSuccess(true);
        return ret;
    }

    public static JSONResult writeFailure(int code, String message) {
        JSONResult ret = new JSONResult();
        ret.setCode(code);
        ret.setSuccess(false);
        ret.setMessage(message);
        return ret;
    }

    public static JSONResult writeFailure(Exceptions es) {
        return writeMessage(es, false);
    }

    public static JSONResult writeSuccess(Exceptions es) {
        return writeMessage(es, true);
    }

    public static JSONResult writeMessage(Exceptions es, Boolean success) {
        JSONResult ret = new JSONResult();
        ret.setSuccess(success);
        ret.setCode(es.getCode());
        ret.setMessage(es.getMessage());
        return ret;
    }
}
